<?php

class Controller_DeviceInfo extends Controller_Website {

    public function action_list() {
        $where = array();
        $where['status'] = Arr::get($_GET, 'status');
        $where['is_recommend'] = Arr::get($_GET, 'is_recommend');
        $where['cid'] = Arr::get($_GET, 'cid');
        $where['ORDER'] = 'id DESC';
        $where = array_filter($where, 'strlen');
        
        $name = Arr::get($_GET, 'name');
        if (!empty($name)) {
            $where['name'] = array('like'=>"%$name%");
        }
        
        $m_deviceInfo = Model::factory('deviceInfo');
        $total = $m_deviceInfo->count($where);
        $pager = new Pager($total, 10);
        $list = $m_deviceInfo->select($pager->offset, $pager->size, $where)->as_array();
        
        $m_deviceCategory = Model::factory('deviceCategory');
        $deviceCategory = $m_deviceCategory->getAll();
        
        $this->content = View::factory('deviceInfo/list');
        $this->content->list = $list;
        $this->content->pager = $pager;
        $this->content->deviceCategory = $deviceCategory;
    }

    public function action_add() {
        $m_deviceInfo = Model::factory('deviceInfo');
        
        if (!empty($_POST)) {
            $data = $this->_get_data($_POST);
            
            $data['add_time'] = time();
            //多张图片拼接
            $data['pic'] = implode(",",$data['pic']);
//            var_dump($data);exit;
            $ret = $m_deviceInfo->insert($data);
            if ($ret !== false) {
                $this->redirect('deviceInfo/list');
            }
        }
        
        $m_deviceCategory = Model::factory('deviceCategory');
        $cat_list = $m_deviceCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        $m_device_brand = Model::factory('deviceBrands');
        $device_brand = $m_device_brand->getAll(array('device_cid'=>1));
        $m_city = Model::factory('city');
        $m_allCity = Model::factory('allCity');
        $city = $m_allCity::index();
        
        $this->content = View::factory('deviceInfo/add');
        $this->content->info = null;
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->city = $city;
        $this->content->device_brand = $device_brand;
    }

    public function action_edit() {
        $id = Arr::get($_GET, 'id');
        $m_deviceInfo = Model::factory('deviceInfo');
        $info = $m_deviceInfo->getRowById($id);
        
        if (!empty($_POST)) {
            $data = $this->_get_data($_POST);
            $ret = $m_deviceInfo->updateById($data, $id);
            if ($ret !== false) {
                $this->redirect('deviceInfo/list');
            }
        }
        
        $m_deviceCategory = Model::factory('deviceCategory');
        $cat_list = $m_deviceCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        
        $m_city = Model::factory('city');
        $m_allCity = Model::factory('allCity');
        $city = $m_allCity::index();
        
        $this->content = View::factory('deviceInfo/edit');
        $this->content->info = $info;
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->city = $city;
    }

    public function action_del() {
        $id = $_GET['id'];
        $m_deviceInfo = Model::factory('deviceInfo');
        $ret = $m_deviceInfo->deleteById($id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }

    public function action_close() {
        $id = $_GET['id'];
        $data = array(
            'status' => 2,
        );
        $m_deviceInfo = Model::factory('deviceInfo');
        $ret = $m_deviceInfo->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_open() {
        $id = $_GET['id'];
        $data = array(
            'status' => 1,
        );
        $m_deviceInfo = Model::factory('deviceInfo');
        $ret = $m_deviceInfo->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }

    protected function _get_data($post) {
        $data = array_intersect_key($post, array_flip(array('cid','name','brands','pic','province','city','area','price','desc','configuration','address')));
        /* if (!empty($data['pic'])) {
            $data['pic'] = array_pop($data['pic']);
        } */
        $data['edit_time'] = strtotime('now');
        return $data;
    }
    
}

